<?php
namespace app\admin\controller;

use app\common\lib\user_operate\ModuleOperate;
use cmf\controller\AdminBaseController;
use think\Hook;

/**
 * 应用市场
 */
class AppDevelopController extends AdminBaseController
{
    private $particularURL = ""; // 应用市场列表-详情

    public $pluginModuleId;

    private $appListURL = ""; // 应用市场列表

    public function _initialize()
    {
        parent::_initialize();

        $data = $this->request->action();

        if ($data == 'default') {

            return show_message($icon = 0, $title = '错误提示', $content = 'REQUEST CONTROLLER IS NOT EXISTS ! ');
        }

        $honstAddress = Config('admin_module_config.api_host');

        $this->particularURL = "$honstAddress/api/api_module_store/particular?"; // 应用市场列表-详情
    }

    /**
     * 配置检查项
     */
    protected $checkItem = [
        0 => [
            'item' => 'isBought',
        ],
        1 => [
            'item' => 'moduleFileExist',
        ],
    ];

    /**
     * 获取检查项
     * @return [type] [description]
     */
    public function getCheckItem()
    {
        return $this->checkItem;
    }

    public function index()
    {
        $param = $this->request->param();

        $mo = new ModuleOperate;

        $List = $mo->getModuleList(); //列表数据
// dump($List);
        if ($List['status'] != 'success') {

            return api_json_echo(false, '获取失败!', null, 100);
        }

        $data = $List['data'];

        $datas = $List['data'];

        //判断每个模块是否安装
        foreach ($data as $k => $v) {

            Hook::listen('plugin_menulist_hook', $v);

            $data[$k] = $v;
        }

        $search = isset($param['search']) ? $param['search'] : null;
        //搜索查询数组
        if (!empty($search)) {

            $data = array_filter($data, function ($t) use ($search) {return $t['store_id'] == $search;});
        }

        $this->assign('search', $search);

        $this->assign('datas', $datas);

        $this->assign('data', $data);

        return $this->fetch();
    }

    /**
     * 安装页面
     */
    public function install()
    {
        $param = $this->request->param();

        $this->assign('data', $param);

        return $this->fetch('app_develop/install');
    }

    /**
     * 安装操作
     */
    public function installOperate()
    {
        $param = $this->request->param();

        $params['info'] = $param;

        Hook::listen('plugin_install_hook', $params);

        if ($params['return'] != true) {
            return api_json_echo(false, $params['return']['message'], null, 201);
        }
        //安装
        cache(null, 'admin_menus'); // 删除后台菜单缓存

        return api_json_echo(true, '安装成功!', null, 200);
    }

    /**
     * 检测
     */
    public function testing()
    {
        $param = $this->request->param();

        $params['info'] = $param;

        Hook::listen('plugin_testing_hook', $params);

        return $params['return'];
    }

    /**
     * 管理页面
     */
    public function adminisTration($moduleName)
    {
        $moduleName = cmf_parse_name($moduleName);

        $AdminIndex = ROOT_PATH . '/public/plugins/' . $moduleName . '/controller/AdminIndexController.php';

        //模块控制器文件不存在
        if (!file_exists($AdminIndex)) {

            return api_json_echo(false, '控制器文件缺失!', null, 100);
        }

        return api_json_echo(true, "控制器检测存在", "{:cmf_plugin_url(cmf_parse_name($moduleName,1).'://adminIndex/index')}", 200);
    }

    /**
     *卸载模块操作
     */
    public function unInstallOperate()
    {
        $param = $this->request->param();

        $params['info'] = $param;

        Hook::listen('plugin_unInstall_hook', $params);

        if ($params['return'] != true) {

            return api_json_echo(false, $params['return'], null, 201);
        }

        return api_json_echo(true, '卸载成功!', null, 200);
    }

    /**
     * 更新模块
     */
    public function updateOperate()
    {
        $param = $this->request->param();

        $params['info'] = $param;

        Hook::listen('plugin_update_hook', $params);

        //执行更新操作
        if ($params['return'] !== true) {

            return api_json_echo(false, $params['return'], null, 201);
        }

        return api_json_echo(true, '更新成功!', null, 200);
    }

    /**
     * 状态返回
     * @return [type] [description]
     */
    protected static $returnStrArray = [];

    private static function returnStr($item = '', $msg = '', $code = 200, $operate = 'empty')
    {
        $data = [
            'item'    => $item,
            'msg'     => $msg,
            'code'    => $code,
            'operate' => $operate,
        ];

        self::$returnStrArray[] = $data;
    }

}
